package activiti2;

import org.activiti.engine.HistoryService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.TaskService;
import org.activiti.engine.history.HistoricVariableInstance;
import org.activiti.engine.task.Task;
import org.junit.Test;

import java.util.Date;
import java.util.List;

/**
 * 流程变量的设置和获取
 */
public class LiuChengBianLiang {

    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

    /**
     * 设置流程变量
     *
     * 流程变量的设置方式有两种，一是通过基本类型设置，第二种是通过JavaBean类型设置。
     */

    //基本类型
    @Test
    public void setProcessVariables(){
        String processInstanceId = "17501";//流程实例ID
        String assignee = "张三";//任务办理人
        TaskService taskService = processEngine.getTaskService();//获取任务的Service，设置和获取流程变量

        //查询当前办理人的任务ID
        Task task = taskService.createTaskQuery()
                .processInstanceId(processInstanceId)//使用流程实例ID
                .taskAssignee(assignee)//任务办理人
                .singleResult();
        //设置流程变量【基本类型】
        taskService.setVariable(task.getId(),"请假人",assignee);
        taskService.setVariableLocal(task.getId(),"请假天数",3);
        taskService.setVariableLocal(task.getId(),"请假日期",new Date());

    }

    //JavaBean类型
    @Test
    public void setProcessVariables2(){
        String processInstanceId = "17501";
        String assginee = "张三";
        TaskService taskService = processEngine.getTaskService();
        //查询当前办理人的任务ID
        Task task = taskService.createTaskQuery()
                .processInstanceId(processInstanceId)
                .taskAssignee(assginee)
                .singleResult();
        //设置流程变量【javabean类型】
        Person p = new Person();
        p.setId("1");
        p.setName("鲍康");
        taskService.setVariable(task.getId(), "人员信息", p);
        System.out.println("流程变量设置成功~~");

    }

    /**
     * 获取流程变量
     *
     * 分两种类型
     */

    //基本类型
    @Test
    public void findProcessVariables(){
        String processInstanceId = "17501";
        String assginee = "张三";
        TaskService taskService = processEngine.getTaskService();
        Task task = taskService.createTaskQuery()
                .processInstanceId(processInstanceId)
                .taskAssignee(assginee)
                .singleResult();
        String person = (String) taskService.getVariable(task.getId(),"请假人");
        Integer day = (Integer) taskService.getVariable(task.getId(),"请假天数");
        Date date = (Date) taskService.getVariable(task.getId(),"请假日期");
        System.out.println(person + " " + day + " " + date);

    }

    //JavaBean类型

    @Test
    public void findProcessVariables2(){
        String processInstanceId = "17501";
        String assginee = "张三";
        TaskService taskService = processEngine.getTaskService();
        Task task = taskService.createTaskQuery()
                .processInstanceId(processInstanceId)
                .taskAssignee(assginee)
                .singleResult();
        Person person = (Person)taskService.getVariable(task.getId(),"人员信息");
        System.out.println(person.getId() + " " + person.getName());

    }

    /**
     * 查询历史流程变量
     */
    @Test
    public void getHistoryProcessVariables(){
        HistoryService historyService =  processEngine.getHistoryService();
        List<HistoricVariableInstance> list = historyService
                .createHistoricVariableInstanceQuery()//创建一个历史的流程变量查询
                .variableName("人员信息")
                .list();
        if(list != null && list.size()>0){
            for (HistoricVariableInstance hvi : list) {
                System.out.println(hvi.getTaskId()+" "+hvi.getVariableName()+" "+hvi.getValue()+" "+hvi.getVariableTypeName());
            }
        }
    }
}
